/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.thinkgem.jeesite.modules.sys.service;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.thinkgem.jeesite.modules.sys.entity.Money;
import com.thinkgem.jeesite.modules.sys.dao.MoneyDao;

/**
 * 金额流水Service
 * @author Jakemanse
 * @version 2013-07-30
 */
@Component
@Transactional(readOnly = true)
public class MoneyService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(MoneyService.class);
	
	@Autowired
	private MoneyDao moneyDao;
	
	public Money get(Long id) {
		return moneyDao.findOne(id);
	}
	
	public Page<Money> find(Page<Money> page, Money money) {
		DetachedCriteria dc = moneyDao.createDetachedCriteria();
		if (money.getUser()!=null && money.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", money.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(money.getName())){
			dc.add(Restrictions.like("name", "%"+money.getName()+"%"));
		}
		if (StringUtils.isNotEmpty(money.getRemarks())){
			dc.add(Restrictions.like("remarks", "%"+money.getRemarks()+"%"));
		}
		dc.add(Restrictions.eq("delFlag", Money.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return moneyDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(Money money) {
		moneyDao.save(money);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		moneyDao.deleteById(id);
	}
	
}
